package com.yuezhu.global;

import android.app.Application;
import android.content.Context;
import android.os.Looper;
import android.widget.Toast;

import com.igexin.sdk.PushConsts;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.Tag;

import java.util.Map;

/**
 * @创建人：zhaoshuzhen
 * @创建时间：2015/12/8 10:10
 */
public class ApplicationContext extends Application {
	private static ApplicationContext app;
	private static Context context;
	private int screen_width;
	// 用于存放倒计时时间
	public static Map<String, Long> map;


	@Override
	public void onCreate() {
		super.onCreate();
		app = this;
		context = getApplicationContext();
		// 百度定位初始化
//		SDKInitializer.initialize(getApplicationContext());

//		initGeTui();
//		Thread.setDefaultUncaughtExceptionHandler(app);
	}

	public static ApplicationContext getInstance() {
		return app;
	}

	public static Context getContext() {
		return context;
	}

	// 个推初始化
	private void initGeTui() {
		PushManager.getInstance().initialize(this.getApplicationContext());

		String token = PushManager.getInstance().getClientid(
				this.getApplicationContext());
		if (token == null) {
			token = token + "";
		}
		System.out.println(token + "---HH----");
		// String[] tags = token.split(",");
		String[] tags = { "上海", "北京", "天津" };
		Tag[] tagParam = new Tag[tags.length];
		for (int i = 0; i < tags.length; i++) {
			Tag t = new Tag();
			t.setName(tags[i]);
			tagParam[i] = t;
		}
		int i = PushManager.getInstance().setTag(this, tagParam);
		/*PushManager.getInstance().bindAlias(context,
				UserMsg.getInstance().getId() + "");*/
		String text = "";
		switch (i) {
		case PushConsts.SETTAG_SUCCESS:
			text = "设置标签成功";
			break;

		case PushConsts.SETTAG_ERROR_COUNT:
			text = "设置标签失败, tag数量过大, 最大不能超过200个";
			break;

		case PushConsts.SETTAG_ERROR_FREQUENCY:
			text = "设置标签失败, 频率过快, 两次间隔应大于1s";
			break;

		case PushConsts.SETTAG_ERROR_REPEAT:
			text = "设置标签失败, 标签重复";
			break;

		case PushConsts.SETTAG_ERROR_UNBIND:
			text = "设置标签失败, 服务未初始化成功";
			break;

		case PushConsts.SETTAG_ERROR_EXCEPTION:
			text = "设置标签失败, 未知异常";
			break;

		case PushConsts.SETTAG_ERROR_NULL:
			text = "设置标签失败, tag 为空";
			break;

		default:
			break;
		}
		// MyToast.showDefaltToast(text);
	}

	public int getScreen_width() {
		return context.getResources().getDisplayMetrics().widthPixels;
	}

	public int getScreen_height() {
		return context.getResources().getDisplayMetrics().heightPixels;
	}
	/**
	 * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
	 * 
	 * @param ex
	 * @return true:如果处理了该异常信息;否则返回false.
	 */
	private boolean handleException(Throwable ex) {
		if (ex == null) {
			return false;
		}
		// 使用Toast来显示异常信息
		new Thread() {
			@Override
			public void run() {
				Toast.makeText(app, "很抱歉,程序出现异常,即将退出并重启", Toast.LENGTH_SHORT)
						.show();
				Looper.loop();
			}
		}.start();
		return true;
	}

}